/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.createdtmassmethodgen;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.bizentity.beenum.BETriggerTimePointType;
import com.inspur.edp.bef.bizentity.operation.BizCommonDetermination;
import com.inspur.edp.bef.bizentity.operation.Determination;
import com.inspur.edp.bef.bizentity.operation.Validation;
import com.inspur.edp.bef.core.determination.builtinimpls.BefAfterModifyDtmAssembler;
import com.inspur.edp.bef.core.determination.builtinimpls.adaptors.BefAftModifyDtmAdaptor;
import com.inspur.edp.bef.core.determination.builtinimpls.adaptors.BefAftModifyPredicateDtmAdaptor;
import com.inspur.edp.bef.core.determination.builtinimpls.predicate.ExecutePredicateFactory;
import com.inspur.edp.bef.core.validation.builtinimpls.adaptors.BefAftModifyValAdaptor;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.operation.ChildDtmTriggerInfo;
import com.inspur.edp.cef.designtime.api.operation.CommonDetermination;
import com.inspur.edp.cef.designtime.api.operation.ExecutingDataStatus;
import com.inspur.edp.cef.entity.changeset.ChildElementsTuple;
import com.inspur.edp.cef.entity.changeset.Util;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityImplGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.createdtmassmethod.EntityCreateAfterModifyDtmAssMethodGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.createdtmassmethod.EntityCreateDtmAssMethodGenerator;
import com.inspur.edp.cef.generator.overall.GenUtils;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import com.inspur.edp.common.component.actionextension.ChildPathTreeAfterModifyDtmAdaptor;
import com.inspur.edp.common.component.actionextension.DeleteDtmAdaptor;
import com.inspur.edp.common.component.actionextension.TreeBeforeSaveDtmAdaptor;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.das.commonmodel.entity.GspCommonObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Statement;

public class BefCreateAfterModifyDtmAssMethodGenerator extends
    EntityCreateAfterModifyDtmAssMethodGenerator {

  public BefCreateAfterModifyDtmAssMethodGenerator(EntityImplGenContext entityImplGenContext,
      String nodeCode) {
    super(entityImplGenContext, nodeCode);
  }

  @Override
  protected TypeInfo getNewAdaptor_TypeParam(CommonDetermination op) {
    return CreateDtmAssMethodUtil.anyChildRequestElement(op)
        ? new TypeInfo(BefAftModifyDtmAdaptor.class)
        : new TypeInfo(BefAftModifyPredicateDtmAdaptor.class);
  }

  @Override
  public boolean skip() {
    return super.skip() && buildAutoAddingStatements().isEmpty();
  }

  @Override
  protected void buildExtendNewAdaptorParameter(CommonDetermination val, List args) {
    if(CreateDtmAssMethodUtil.anyChildRequestElement(val)) {
      GspBizEntityObject dataType = (GspBizEntityObject) getEntityImplGenContext().getDataType();
      BizCommonDetermination bizCommonDetermination = (BizCommonDetermination)val;
      args.add(Utils.buildChgTypes(ast, val.getGetExecutingDataStatus()));
      args.add(ast.newBooleanLiteral(bizCommonDetermination.getRunOnce()));
      args.add(GenUtils.buildRequestElementsList(ast, getEntityImplGenContext().getDataType(), val.getRequestElements()));
      args.add(Utils.buildRequestChildElements(ast, dataType, val.getRequestChildElements()));
    } else {
      addImport(ExecutePredicateFactory.class.getTypeName());
      args.add(CreateDtmAssMethodUtil.buildPredicate(ast, val, getEntityImplGenContext()));
      args.add(ast.newBooleanLiteral(((BizCommonDetermination) val).getRunOnce()));
    }
//    GspBizEntityObject dataType = (GspBizEntityObject) getEntityImplGenContext().getDataType();
//    Determination v = (Determination) dataType.getDeterminations().getItem(val.getID());
//    args.add(Utils.buildChgTypes(ast, v.getRequestNodeTriggerType()));
//    args.add(ast.newBooleanLiteral(v.getRunOnce()));
//    if(v.getRequestElements().isEmpty() && v.getRequestChildElements().isEmpty()) {
//      return;
//    }
//    args.add(GenUtils.buildRequestElementsList(ast, getEntityImplGenContext().getDataType(), v.getRequestElements()));
//    if(!v.getRequestChildElements().isEmpty()) {
//      args.add(Utils.buildRequestChildElements(ast, dataType, v.getRequestChildElements()));
//    }
  }



  @Override
  protected void addExtendingStatements(Block block) {
    block.statements().addAll(buildAutoAddingStatements());
  }

  private List<Statement> autoAddings;
  private List<Statement> buildAutoAddingStatements() {
    if(autoAddings == null) {
      autoAddings = new ArrayList<>();
      getTreeBeforeSaveDtmGenerators(autoAddings);
    }
    return autoAddings;
  }

  private void getTreeBeforeSaveDtmGenerators(List<Statement> block) {
    IGspCommonObject obj = (IGspCommonObject)getEntityImplGenContext().getDataType();
    if(((GspBusinessEntity)obj.getBelongModel()).getEnableTreeDtm() && obj.getParentObject() != null) {
      for (IGspCommonField element : obj.getContainElements()) {
        if (element.getIsUdt() && element.getUdtID().equals("dbfbe55d-ba65-4a7f-a9d4-4f664ec6ec68")) {
          addImport(new TypeInfo(ChildPathTreeAfterModifyDtmAdaptor.class));
          ClassInstanceCreation adaptorInstanceCreation = GenUtils
              .buildClassInstantiation(ast,
                  ChildPathTreeAfterModifyDtmAdaptor.class.getTypeName());
          MethodInvocation methodInvocation = RepositoryGenUtil
              .createMethodInvocation(ast, ast.newSimpleName(varNameAssembler),
                  "addDetermination", null);
          methodInvocation.arguments().add(adaptorInstanceCreation);
          block.add(ast.newExpressionStatement(methodInvocation));
          break;
        }
      }
    }
  }
}
